import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:my_flutter_github/MyStyle.dart';
import 'package:my_flutter_github/common/dao/ReposDao.dart';
import 'package:my_flutter_github/common/dao/UserDao.dart';
import 'package:my_flutter_github/common/dao/UserEventDao.dart';
import 'package:my_flutter_github/page/BasePersonState.dart';
import 'package:my_flutter_github/redux/MyState.dart';
import 'package:my_flutter_github/redux/UserRedux.dart';
import 'package:my_flutter_github/widgets/PullLoadWidget.dart';
import 'package:redux/redux.dart';

///我的界面
class MineTabPage extends StatefulWidget {
  @override
  _MineTabPageState createState() => _MineTabPageState();
}

class _MineTabPageState extends BasePersonState<MineTabPage> {
  String beStaredCount = '---';
  Color notifyColor = const Color(MyColors.subTextColor);

  Store<MyState> _getStore() {
    return StoreProvider.of(context);
  }

  _getUserName() {
    if (_getStore().state.userInfo == null) {
      return null;
    }
    return _getStore().state.userInfo.login;
  }

  getUserType() {
    if (_getStore().state.userInfo == null) {
      return null;
    }
    return _getStore().state.userInfo.type;
  }

  _refreshNotify() {
    UserDao.getNotifyDao(false, false, 0).then((res) {
      Color newColor;
      if (res != null && res.result && res.data.length > 0) {
        newColor = Color(MyColors.actionBlue);
      } else {
        newColor = Color(MyColors.subLightTextColor);
      }
      if (isShow) {
        setState(() {
          notifyColor = newColor;
        });
      }
    });
  }

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    pullLoadWidgetControl.needHeader = true;
    super.initState();
  }

  _getDataLogic() async {
    print("_getDataLogic.userType:${getUserType()}");
    if (getUserType() == "Organization") {
      return await UserDao.getMemberDao(_getUserName(), pageIndex);
    }
    return await UserEventDao.getEventDao(_getUserName(),
        page: pageIndex, needDb: pageIndex <= 1);
  }

  @override
  requestRefresh() async {
    print("requestRefresh");
    UserDao.getUserInfo(null).then((res) {
      if (res != null && res.result) {
        _getStore().dispatch(UpdateUserAction(res.data));
        getUserOrg(_getUserName());
      }
    });

    ReposDao.getUserRepository100StatusDao(_getUserName()).then((res) {
      if (res != null && res.result) {
        if (isShow) {
          setState(() {
            beStaredCount = res.data.toString();
            print("beStaredCount=:$beStaredCount");
          });
        }
      }
    });
    _refreshNotify();
    return await _getDataLogic();
  }

  @override
  requestLoadMore() async {
    print("requestLoadMore");
    return await _getDataLogic();
  }

  @override
  bool get autoRefresh => false;

  @override
  bool get needHeader => true;

  @override
  void didChangeDependencies() {
    if (pullLoadWidgetControl.dataList.length == 0) {
      showRefreshLoading();
    }
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return StoreBuilder<MyState>(builder: (context, store) {
      return PullLoadWidget(
        itemBuilder: (context, index) {
          return renderItem(index, store.state.userInfo, beStaredCount, notifyColor, (){
            _refreshNotify();
          }, orgList);
        },
        refreshCallback: onRefresh,
        loadMoreCallback: onLoadMore,
        control: pullLoadWidgetControl,
        refreshKey: refreshIndicatorKey,
      );

    });
  }
}
